package com.example.comhnevcshenhaibing;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class denglu extends AppCompatActivity {

    private EditText username;
    private EditText password;
    private Button queding;

    private static final String TAG = "denglu";
    private String name;
    private String word;
    private String ding;
    private String huce;
    private String url;
    private TextView shoujizhuce;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.denglu);
        initView();
        initDate();
        initEvent();
    }

    private void initView() {
        username = findViewById(R.id.et_username);
        password = findViewById(R.id.et_password);
        queding = findViewById(R.id.btn_queding);
        shoujizhuce = findViewById(R.id.tv_shoujizhuce);
    }

    private void initDate() {
        name = username.getText().toString().trim();
        word = password.getText().toString().trim();
        ding = queding.getText().toString().trim();
        huce = shoujizhuce.getText().toString().trim();
        String url = "http://10.1.62.39/api/user/login";
        final JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("telephone", name);
            jsonObject.put("password", word);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestQueue queue = Volley.newRequestQueue(denglu.this);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.e(TAG, "onResponse: " + jsonObject);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG, "onErrorResponse: " + volleyError.getMessage());
            }
        });
        queue.add(jsonObjectRequest);
    }

    private void initEvent() {
        queding.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(denglu.this, "登陆成功", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(denglu.this, pinglun.class);
                startActivity(intent);
            }
        });
        shoujizhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(denglu.this, "进入手机注册页面", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(denglu.this, shoujizhuce.class);
                startActivity(intent);
            }
        });
    }
}
